package com.yuexunit.finance.member.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.member.entity.SupplierEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;

/**
*@version 创建时间：2017年4月26日 下午8:45:10
*@author wenwenlong
*类说明
*/

@Implements(SupplierService.class)
public class SupplierServiceImpl implements SupplierService{

	@Override
	@Transactional
	public Long createSupplier(SupplierEntity entity) {
		entity.setEnabled(Boolean.FALSE);
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());	
		PersistUtil.insert(entity);
		return entity.getSupplierId();
	}

	@Override
	@Transactional
	public void updateSupplier(SupplierEntity entity) {
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(entity);
	}

	@Override
	@Transactional
	public void deleteSupplier(Long supplierId) {
		PersistUtil.delete(SupplierEntity.class, supplierId);
	}

	@Override
	public SupplierEntity inquireSupplierDetail(Long supplierId) {
		return PersistUtil.get(SupplierEntity.class, supplierId);
	}

	@Override
	public List<SupplierEntity> inquireSupplierList() {
		DetachedCriteria criteria = DetachedCriteria.forClass(SupplierEntity.class);
		return PersistUtil.get(criteria);
	}

	@Override
	public PageList<SupplierEntity> inquireSupplierPageList(Long enterpriseId, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(SupplierEntity.class);
		if (enterpriseId != null) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		return PersistUtil.get(criteria, page);
	}

	@Override
	public SupplierEntity inquireSupplierDetail(String supplierName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(SupplierEntity.class);
		criteria.add(Restrictions.eq("supplierName", supplierName));
		List<SupplierEntity> list = PersistUtil.get(criteria);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}
}
